//
//  MenuTuijianChangjingDetailViewController.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//

//   http://api.izhangchu.com/
//   methodName=SceneInfo&scene_id=181&token=&user_id=&version=4.4
#import "MenuTuijianChangjingDetailViewController.h"
#import "MenuTuiJianChangJingDetailModel.h"
#import "MenuTuiJianChangJingDetailTableViewCell.h"
#import "MenuFirstDetailViewController.h"
#import "MenuTuiJianXhangJiangDetailCollectionReusableView.h"
#import "MenuTuiJianChangJingViewController.h"

#import "MenuTuiJianChangJingModel.h"
#import "MenuTuiJianChangjingCollectionViewCell.h"
#import "MenuFirstModel.h"

@interface MenuTuijianChangjingDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UIImageView *picImageView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)UILabel *countLabel;

@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, strong)NSMutableArray *downModelArray;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSString *picString;
@property (nonatomic, strong)NSNumber *countNum;
@property (nonatomic, strong)NSString *contentString;

@property (nonatomic, strong)UICollectionView *collectionView;

//  导航栏 上
@property (nonatomic, strong)NSString *titleString;
//  分享
@property (nonatomic, strong)NSString *shareString;
@end

@implementation MenuTuijianChangjingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = YES;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight ) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource  =self;
    self.tableView.rowHeight = 100;
    [self.tableView registerClass:[MenuTuiJianChangJingDetailTableViewCell class] forCellReuseIdentifier:@"MenuTuiJianChangJingDetailTableViewCell"];
    [self.view addSubview:_tableView];
    // Do any additional setup after loading the view.
    
    self.picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    
    self.countLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidth/2 - 50,160 , 100,20 )];
    self.countLabel.font = [UIFont systemFontOfSize:13];
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    
    self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 180, kWidth - 20, 20)];
    self.contentLabel.font = [UIFont systemFontOfSize:13];
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    [self.picImageView addSubview:_countLabel];
    [self.picImageView addSubview:_contentLabel];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight - 200, kWidth, 190) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[MenuTuiJianChangjingCollectionViewCell class] forCellWithReuseIdentifier:@"MenuTuiJianChangjingCollectionViewCell"];
    [self.collectionView registerClass:[MenuTuiJianXhangJiangDetailCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MenuTuiJianXhangJiangDetailCollectionReusableView"];
    self.tableView.tableFooterView = _collectionView;
    
    self.tableView.tableHeaderView = _picImageView;
    
    [self requestData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.downModelArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kWidth/ 3-1, 150);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderViewAction:)];
        MenuTuiJianXhangJiangDetailCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MenuTuiJianXhangJiangDetailCollectionReusableView" forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(kWidth/ 2 - 150, 10, 300, 30)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"更多场景";
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(kWidth - 60, 15, 25, 20);
        [button setBackgroundImage:[UIImage imageNamed:@"menutiaozhuan"] forState:(UIControlStateNormal)];
        [headerView addSubview:button];
        [headerView addGestureRecognizer:tap];
        
        [headerView addSubview:label];
      
        return headerView;
    }
    return nil;
}
- (void)sectionHeaderViewAction:(UITapGestureRecognizer *)tap{
    MenuTuiJianChangJingViewController *changjingVc = [[MenuTuiJianChangJingViewController alloc]init];
    [self.navigationController pushViewController:changjingVc animated:YES];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kWidth, 40);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuTuiJianChangjingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuTuiJianChangjingCollectionViewCell" forIndexPath:indexPath];
    MenuTuiJianChangJingModel *model = self.downModelArray[indexPath.row];
    cell.model = model;
    
    cell.countLabel.text = [NSString stringWithFormat:@"%@道菜",model.dishes_count];
    return cell;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
        return self.modelArray.count;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        MenuTuiJianChangJingDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuTuiJianChangJingDetailTableViewCell" forIndexPath:indexPath];
    MenuTuiJianChangJingDetailModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
    
   
}

#pragma mark - 点击cell -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuTuijianChangjingDetailViewController *Vc = [[MenuTuijianChangjingDetailViewController alloc]init];
    MenuTuiJianChangJingModel*model = self.downModelArray[indexPath.row];
    Vc.scene_id = model.scene_id;
    [self.navigationController pushViewController:Vc animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuFirstDetailViewController *detailVc = [[MenuFirstDetailViewController alloc]init];
    MenuTuiJianChangJingDetailModel *deModel = self.modelArray[indexPath.row];
//    MenuFirstModel *model = [[MenuFirstModel alloc]init];
//    model.title = deModel.dishes_name;
//    model.dishes_id = deModel.dishes_id;
//    model.image = deModel.image;
//    model.bigSpArray = deModel.array;
//    model.content = deModel.dishes_desc;
    detailVc.dishes_id = deModel.dishes_id;
    [self.navigationController pushViewController:detailVc animated:YES];
}
- (void)requestData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic: @{@"methodName":@"SceneInfo",@"scene_id":self.scene_id,@"token":@"",@"user_id":@"",@"&version":@"4.4"} finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.modelArray = [MenuTuiJianChangJingDetailModel modelConfigureWithDic:jsonDic];
        self.countNum = jsonDic[@"data"][@"dish_count"];
        self.titleString = jsonDic[@"data"][@"scene_title"];
        self.picString = jsonDic[@"data"][@"scene_background"];
        self.contentString = jsonDic[@"data"][@"scene_desc"];
        self.shareString = jsonDic[@"data"][@"share_url"];
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.picString] placeholderImage:nil completed:nil];
        self.countLabel.text = [NSString stringWithFormat:@"%@道菜",self.countNum];
        self.contentLabel.text = self.contentString;
        
        self.downModelArray = [MenuTuiJianChangJingModel modelDetailConfigureWithDic:jsonDic];
        NSLog(@"%@",self.downModelArray);
        [self.collectionView reloadData];
        [self.tableView reloadData];
    } error:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
